﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using DalatCooking.Core;
using DalatCooking.Core.Helper;
public partial class admin_Person_Roles : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {

        if (!IsPostBack)
        {
            BindGrid();
        }
        gvPolls.RowCancelingEdit += delegate { Response.Redirect(Request.RawUrl); };
    }
    protected override void OnInit(EventArgs e)
    {
        if (!IsPostBack)
        {
            Person p = Session.GetPersonLogged();
            if (!p.IsInRole("SuperUser"))
                Response.Redirect("/admin/accessdeny.aspx");
        }
        base.OnInit(e);
    }
    private void BindGrid()
    {
        gvPolls.DataSource = RoleManager.FindAll();
        gvPolls.DataBind();
    }

    protected void gvPolls_RowDeleting(object sender, GridViewDeleteEventArgs e)
    {
        int pollID = int.Parse(gvPolls.DataKeys[e.RowIndex]["RoleID"].ToString());

        RoleManager.Delete(pollID);

        divMsg.InnerHtml = "<b>Thông tin:</b> Xóa role thành công.";
        divMsg.Visible = true;
        BindGrid();
    }
    public int Count()
    {

        return gvPolls.Rows.Count;
    }
    protected void gvPolls_RowEditing(object sender, GridViewEditEventArgs e)
    {
        gvPolls.EditIndex = e.NewEditIndex;
        BindGrid();
    }
    protected void gvPolls_RowUpdating(object sender, GridViewUpdateEventArgs e)
    {
        int id = (int)gvPolls.DataKeys[e.RowIndex].Value;
        TextBox txtRole = (TextBox)gvPolls.Rows[e.RowIndex].FindControl("txtRole");
        Role r = RoleManager.Single(id);
        r.RoleName = txtRole.Text;
        if (RoleManager.Update(r))
            Response.Redirect(Request.RawUrl);
    }

    protected void gvPolls_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        if (e.Row.RowState == DataControlRowState.Edit ||
               e.Row.RowState == (DataControlRowState.Alternate | DataControlRowState.Edit))
        {
            Role self = (Role)e.Row.DataItem;

        }
    }
    protected void btnAdd_Click(object sender, EventArgs e)
    {
        Role r = new Role();
        r.RoleName = txtRoleName.Text;
        if (RoleManager.Insert(r))
            Response.Redirect(Request.RawUrl);
    }
}